# -*- coding:utf-8 -*-
from common import command_base, response_base, error_response, result_object
from common import common_strings
from global_vars import g_dbsession
from common.model.user import user_info
class login_command(command_base):
    def __init__(self, obj_dec, socket):
        super(login_command, self).__init__(obj_dec, socket)
    def execute(self):
        if not self._command_body.has_key(common_strings.USER_NAME) or \
            not self._command_body.has_key(common_strings.USER_PASSWORD):
            self.sendResponse(error_response(self._command_id))
            return
        username = self._command_body[common_strings.USER_NAME]
        passwd = self._command_body[common_strings.USER_PASSWORD]
        retobj = result_object()
        query = g_dbsession.query(user_info)
        user = query.select_by(user_name=username)
        if not user:
            retobj.result = False; retobj.errstr = u'找不到该用户'
            return retobj
        if user.c.password != passwd:
            retobj.result = False; retobj.errstr = u'用户密码错误'
            return retobj
        from datetime import datetime
        user.c.login_time = datetime.now()
        
class login_response(response_base):
    def __init__(self): pass
